/**
 * 0. Project	: WishList 프로젝트
 *
 * 1. FileName	: DatePickerFragment.java
 * 2. Package	: com.jjworx.fragment
 * 3. 작성자		: 윤화현
 * 4. 작성일		: 2013. 7. 10. 오후 1:05:21
 * 5. Comment	: 
 */

package com.dnbns.bodywater;

import java.util.Calendar;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

import com.dnbns.common.StaticValue;
import com.dnbns.lib.RbPreference;

@SuppressLint("ValidFragment")
public class WeightDialogFragment extends DialogFragment {

	private NoticeDialogListener m_Listener;
	private int DIALOG_TYPE;
	private float mWeight;
	private String mBirth;
	private int idx;
	private int mHour;
	private int mMinute;

	private boolean mScrollWhell = false;
	
	public interface NoticeDialogListener {
		//확인 다이얼로그. 
        public void onDialogNegativeClick(DialogFragment dialog);
        public void onDialogTimeClick(DialogFragment dialog, int idx, String hour, String minute);
    }
	
	public WeightDialogFragment(int DIALOG_TYPE) {
		// TODO Auto-generated constructor stub
		this.DIALOG_TYPE = DIALOG_TYPE;
	}

	//생년월일
	public WeightDialogFragment(int DIALOG_TYPE, String mBirth) {
		// TODO Auto-generated constructor stub
		this.DIALOG_TYPE = DIALOG_TYPE;
		this.mBirth = mBirth;
	}

	//몸무게
	public WeightDialogFragment(int DIALOG_TYPE, float mWeight) {
		// TODO Auto-generated constructor stub
		this.DIALOG_TYPE = DIALOG_TYPE;
		this.mWeight = mWeight;
	}

	//시간
	public WeightDialogFragment(int DIALOG_TYPE, int idx, int mHour, int mMinute) {
		// TODO Auto-generated constructor stub
		this.idx = idx;
		this.DIALOG_TYPE = DIALOG_TYPE;
		this.mHour = mHour;
		this.mMinute = mMinute;
	}

	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            m_Listener = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
        }
    }
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState){
		Dialog dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		
		//생년월일 선택
		if(DIALOG_TYPE == StaticValue.DIALOG_BIRTH)
		{
			LayoutInflater m_Inflater = getActivity().getLayoutInflater();
			View view = m_Inflater.inflate(R.layout.dialog_birth, null);

			final WheelView mYearWheel = (WheelView) view.findViewById(R.id.year);
			final WheelView mMonthWheel = (WheelView) view.findViewById(R.id.month);
			final WheelView mDayWheel = (WheelView) view.findViewById(R.id.day);

			// set current time
			Calendar c = Calendar.getInstance();
			int curYear		= c.get(Calendar.YEAR);
			
			final NumericWheelAdapter mYearList = new NumericWheelAdapter(getActivity(), curYear-99, curYear);
			final NumericWheelAdapter mMonthList = new NumericWheelAdapter(getActivity(), 1, 12);
			final NumericWheelAdapter mDayList = new NumericWheelAdapter(getActivity(), 1, 31);
			
			mYearWheel.setViewAdapter(mYearList);
			mMonthWheel.setViewAdapter(mMonthList);
			mDayWheel.setViewAdapter(mDayList);

			String[] temp = mBirth.split("-");
			
			mYearWheel.setCurrentItem(Integer.valueOf(temp[0]) - (curYear-99));
			mMonthWheel.setCurrentItem(Integer.valueOf(temp[1])-1);
			mDayWheel.setCurrentItem(Integer.valueOf(temp[2])-1);

			OnWheelChangedListener wheelListener = new OnWheelChangedListener() {
				public void onChanged(WheelView wheel, int oldValue, int newValue) {
					if(!mScrollWhell){
					}
				}
			};

			OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
				public void onScrollingStarted(WheelView wheel) {
					mScrollWhell = true;
				}

				public void onScrollingFinished(WheelView wheel) {
					mScrollWhell = false;
				}
			};

			mYearWheel.addChangingListener(wheelListener);
			mMonthWheel.addChangingListener(wheelListener);
			mDayWheel.addChangingListener(wheelListener);

			mYearWheel.addScrollingListener(scrollListener);
			mMonthWheel.addScrollingListener(scrollListener);
			mDayWheel.addScrollingListener(scrollListener);

			Button ok_btn = (Button)view.findViewById(R.id.ok_btn);
			ok_btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					RbPreference pref = new RbPreference(getActivity().getApplicationContext());
					
					String birth = mYearList.getItemText(mYearWheel.getCurrentItem()).toString();
					birth = birth + "-" + mMonthList.getItemText(mMonthWheel.getCurrentItem()).toString();
					birth = birth + "-" + mDayList.getItemText(mDayWheel.getCurrentItem()).toString();

					pref.put("birth", birth);
					
					m_Listener.onDialogNegativeClick(WeightDialogFragment.this);
				}
			});
			Button cancel_btn = (Button)view.findViewById(R.id.cancel_btn);
			cancel_btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					m_Listener.onDialogNegativeClick(WeightDialogFragment.this);
				}
			});

			dialog.setContentView(view);
			return dialog;
		} else if (DIALOG_TYPE == StaticValue.DIALOG_WEIGHT){
			LayoutInflater m_Inflater = getActivity().getLayoutInflater();
			View view = m_Inflater.inflate(R.layout.dialog_weight, null);

			final WheelView mWeightWheel = (WheelView) view.findViewById(R.id.weight);
			
			final NumericWheelAdapter mWeightList = new NumericWheelAdapter(getActivity(), 20, 300);
			
			mWeightWheel.setViewAdapter(mWeightList);
			mWeightWheel.setCurrentItem((int) (mWeight-20));

			OnWheelChangedListener wheelListener = new OnWheelChangedListener() {
				public void onChanged(WheelView wheel, int oldValue, int newValue) {
					if(!mScrollWhell){
					}
				}
			};

			OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
				public void onScrollingStarted(WheelView wheel) {
					mScrollWhell = true;
				}

				public void onScrollingFinished(WheelView wheel) {
					mScrollWhell = false;
				}
			};

			mWeightWheel.addChangingListener(wheelListener);
			mWeightWheel.addScrollingListener(scrollListener);

			Button ok_btn = (Button)view.findViewById(R.id.ok_btn);
			ok_btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					RbPreference pref = new RbPreference(getActivity().getApplicationContext());
					pref.put("weight", mWeightList.getItemText(mWeightWheel.getCurrentItem()).toString());
					m_Listener.onDialogNegativeClick(WeightDialogFragment.this);
				}
			});
			Button cancel_btn = (Button)view.findViewById(R.id.cancel_btn);
			cancel_btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					m_Listener.onDialogNegativeClick(WeightDialogFragment.this);
				}
			});

			dialog.setContentView(view);
			return dialog;
		} else if (DIALOG_TYPE == StaticValue.DIALOG_TIME){
			LayoutInflater m_Inflater = getActivity().getLayoutInflater();
			View view = m_Inflater.inflate(R.layout.dialog_time, null);

			final WheelView mHourWheel = (WheelView) view.findViewById(R.id.hour);
			final WheelView mMinuteWheel = (WheelView) view.findViewById(R.id.minute);

			final NumericWheelAdapter mHourList = new NumericWheelAdapter(getActivity(), 1, 24, "%02d");
			final NumericWheelAdapter mMinuteList = new NumericWheelAdapter(getActivity(), 0, 59, "%02d");

			mHourWheel.setViewAdapter(mHourList);
			mHourWheel.setCurrentItem(mHour-1);
			mMinuteWheel.setViewAdapter(mMinuteList);
			mMinuteWheel.setCurrentItem(mMinute);
			
			OnWheelChangedListener wheelListener = new OnWheelChangedListener() {
				public void onChanged(WheelView wheel, int oldValue, int newValue) {
					if(!mScrollWhell){
					}
				}
			};
			
			OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
				public void onScrollingStarted(WheelView wheel) {
					mScrollWhell = true;
				}

				public void onScrollingFinished(WheelView wheel) {
					mScrollWhell = false;
				}
			};

			mHourWheel.addChangingListener(wheelListener);
			mMinuteWheel.addChangingListener(wheelListener);
			mHourWheel.addScrollingListener(scrollListener);
			mMinuteWheel.addScrollingListener(scrollListener);

			Button ok_btn = (Button)view.findViewById(R.id.ok_btn);
			ok_btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					m_Listener.onDialogTimeClick(WeightDialogFragment.this,
							idx,
							mHourList.getItemText(mHourWheel.getCurrentItem()).toString(), 
							mMinuteList.getItemText(mMinuteWheel.getCurrentItem()).toString());
				}
			});
			Button cancel_btn = (Button)view.findViewById(R.id.cancel_btn);
			cancel_btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					m_Listener.onDialogNegativeClick(WeightDialogFragment.this);
				}
			});

			dialog.setContentView(view);
			return dialog;
		}
		
		
		return null;
	}
}